package cn.usts.edu.lesson02;

import java.awt.*;
import java.awt.event.*;

public class Calculator {

    public static void main(String[] args) {
        // 开始
        new MyFrame01();
    }
}

class MyFrame01 extends Frame{

    TextField num1,num2,num3;
    Button button1;

    public MyFrame01(){
        setLocation(400,400);
        setVisible(true);
        // 设置布局
        setLayout(new FlowLayout());
        // 设置文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(10);

//        TextField first = new TextField(10);    // 设置框的大小
//        TextField second = new TextField(10);
//        TextField result = new TextField(10);
        // 按钮
        button1 = new Button("=");

//        Button button = new Button("=");
        // 添加按钮监听
        button1.addActionListener(new CalculatorActionListener01(num1,num2,num3));

        // 添加进frame
        add(num1);
        add(num2);
        add(button1);
        add(num3);

        pack();

        // 点击关闭
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}


// 监听器
class CalculatorActionListener01 implements ActionListener{
    private TextField num1,num2,num3;

    // 构造器
    public CalculatorActionListener01(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int first = Integer.parseInt(num1.getText());
        int second = Integer.parseInt(num2.getText());
        num3.setText(""+(first+second));
        num1.setText("");
        num2.setText("");
    }
}

//class  CalculatorActionListener implements ActionListener {
//    private TextField first,second,result;
//    public CalculatorActionListener(TextField first,TextField second,TextField result) {
//        this.first = first;
//        this.second = second;
//        this.result =result;
//    }
//
//    @Override
//    public void actionPerformed(ActionEvent e) {
//        // 获取字符值
//        int num1 = Integer.parseInt(first.getText());
//        int num2 = Integer.parseInt(second.getText());
//        // 进行运算
//        result.setText(""+(num1+num2));
//        // 返回结果
//        first.setText("");
//        second.setText("");
//
//
//    }
//}

